<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>
</head>

<body>
<div class="article-body">

    <div class="article-intro" id="content">

        <h1>Python3 解释器</h1>
        <p>
            Linux/Unix的系统上，一般默认的 python 版本为 2.x，我们可以将 python3.x 安装在 <strong>/usr/local/python3</strong> 目录中。
        </p>
        <p>安装完成后，我们可以将路径 <strong>/usr/local/python3/bin</strong> 添加到您的 Linux/Unix 操作系统的环境变量中，这样您就可以通过 shell 终端输入下面的命令来启动
            Python3 。
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ PATH</span><span class="pun">=</span><span
                class="pln">$PATH</span><span class="pun">:</span><span class="str">/usr/</span><span
                class="kwd">local</span><span class="pun">/</span><span class="pln">python3</span><span
                class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span
                class="pln">python3    </span><span class="com"># 设置环境变量</span><span class="pln">
$ python3 </span><span class="pun">--</span><span class="pln">version
</span><span class="typ">Python</span><span class="pln"> </span><span class="lit">3.4</span><span
                class="pun">.</span><span class="lit">0</span></pre>
        <p>在Window系统下你可以通过以下命令来设置Python的环境变量，假设你的Python安装在 C:\Python34 下:</p>
        <pre class="prettyprint prettyprinted" style=""><span class="kwd">set</span><span class="pln"> path</span><span
                class="pun">=%</span><span class="pln">path</span><span class="pun">%;</span><span
                class="pln">C</span><span class="pun">:</span><span class="pln">\python34</span></pre>

        <h2>
            交互式编程</h2>
        <p>我们可以在命令提示符中输入"Python"命令来启动Python解释器：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ python3</span></pre>
        <p>执行以上命令后，出现如下窗口信息：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ python3
</span><span class="typ">Python</span><span class="pln"> </span><span class="lit">3.4</span><span
                class="pun">.</span><span class="lit">0</span><span class="pln"> </span><span class="pun">(</span><span
                class="kwd">default</span><span class="pun">,</span><span class="pln"> </span><span
                class="typ">Apr</span><span class="pln"> </span><span class="lit">11</span><span
                class="pln"> </span><span class="lit">2014</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">13</span><span class="pun">:</span><span
                class="lit">05</span><span class="pun">:</span><span class="lit">11</span><span
                class="pun">)</span><span class="pln">
</span><span class="pun">[</span><span class="pln">GCC </span><span class="lit">4.8</span><span
                class="pun">.</span><span class="lit">2</span><span class="pun">]</span><span class="pln"> on linux
</span><span class="typ">Type</span><span class="pln"> </span><span class="str">"help"</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">"copyright"</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">"credits"</span><span
                class="pln"> </span><span class="kwd">or</span><span class="pln"> </span><span
                class="str">"license"</span><span class="pln"> </span><span class="kwd">for</span><span class="pln"> more information</span><span
                class="pun">.</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span></pre>
        <p>在 python 提示符中输入以下语句，然后按回车键查看运行效果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="kwd">print</span><span class="pln"> </span><span
                class="pun">(</span><span class="str">"Hello, Python!"</span><span class="pun">);</span></pre>
        <p>以上命令执行结果如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Hello</span><span class="pun">,</span><span
                class="pln"> </span><span class="typ">Python</span><span class="pun">!</span></pre>
        <p>当键入一个多行结构时，续行是必须的。我们可以看下如下 if 语句：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> flag </span><span class="pun">=</span><span class="pln"> </span><span
                class="kwd">True</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">if</span><span
                class="pln"> flag </span><span class="pun">:</span><span class="pln">
</span><span class="pun">...</span><span class="pln">     </span><span class="kwd">print</span><span
                class="pun">(</span><span class="str">"flag 条件为 True!"</span><span class="pun">)</span><span
                class="pln">
</span><span class="pun">...</span><span class="pln">
flag </span><span class="pun">条件为</span><span class="pln"> </span><span class="kwd">True</span><span
                class="pun">!</span></pre>


        <h2>
            脚本式编程</h2>
        <p>将如下代码拷贝至 <strong>hello.py</strong>文件中：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="kwd">print</span><span class="pln"> </span><span
                class="pun">(</span><span class="str">"Hello, Python!"</span><span class="pun">);</span></pre>
        <p>通过以下命令执行该脚本：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">python3 hello</span><span class="pun">.</span><span
                class="pln">py</span></pre>
        <p>输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Hello</span><span class="pun">,</span><span
                class="pln"> </span><span class="typ">Python</span><span class="pun">!</span></pre>
        <p>
            在Linux/Unix系统中，你可以在脚本顶部添加以下命令让Python脚本可以像SHELL脚本一样可直接执行：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#! /usr/bin/env python3</span></pre>
        <p>然后修改脚本权限，使其有执行权限，命令如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ chmod </span><span class="pun">+</span><span
                class="pln">x hello</span><span class="pun">.</span><span class="pln">py</span></pre>
        <p>执行以下命令：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">./</span><span class="pln">hello</span><span
                class="pun">.</span><span class="pln">py</span></pre>
        <p>输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Hello</span><span class="pun">,</span><span
                class="pln"> </span><span class="typ">Python</span><span class="pun">!</span></pre>
        <p>

        </p></div>
</div>
</body>
</html>
